package com.controller;

import com.beans.Article;
import com.beans.Question;
import com.mapper.QuestionMapper;
import com.server.ArticleService;
import com.server.QuestionService;
import org.apache.ibatis.type.Alias;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * ClassName:QuestionController
 * Package:com.controller
 * Description:
 *
 * @Create:2023/10/24-21:02
 */

@Controller
@RequestMapping("/questions")
public class QuestionController {
    @Autowired
    private QuestionService questionService;

    @Autowired
    private ArticleService articleService;

    @GetMapping("/get/{questionID}")
    public String getQuestion(@PathVariable int questionID, Model model) {
        Question question = questionService.getQuestionById(questionID);
        model.addAttribute("question", question);
        return "testDetails";
    }

    @GetMapping("/get2/{questionID}")
    public String getQuestion2(@PathVariable int questionID, Model model) {
        Question question = questionService.getQuestionById(questionID);
        model.addAttribute("question", question);
        return "questionDetails";
    }
    @GetMapping("/all2")
    public String getAllQuestions2(Model model) {
        List<Question> questions = questionService.getAllQuestions();
        model.addAttribute("questions", questions);
        return "questionList";
    }


    @GetMapping("/all")
    public String getAllQuestions(Model model) {
        List<Question> questions = questionService.getAllQuestions();
        model.addAttribute("questions", questions);
        return "testList";
    }

    @GetMapping("/add")
    public String showAddQuestionForm(Model model) {
        List<Article> articles = articleService.getAllArticles(); // 获取文章列表
        model.addAttribute("articles", articles); // 将文章列表添加到模型

        model.addAttribute("question", new Question());
        return "addQuestion";
    }

    @PostMapping("/add")
    public String addQuestion(@ModelAttribute("question") Question question, ModelMap model) {
        questionService.addQuestion(question);
        model.addAttribute("message", "问题添加成功");
        return "redirect:/questions/all2";
    }

    @GetMapping("/edit/{questionID}")
    public String showEditQuestionForm(@PathVariable int questionID, Model model) {
        Question question = questionService.getQuestionById(questionID);
        model.addAttribute("question", question);
        return "editQuestion";
    }

    @PostMapping("/edit/{questionID}")
    public String editQuestion(@PathVariable int questionID, @ModelAttribute("question") Question updateSelective, ModelMap model) {
        Question originalQuestion = questionService.getQuestionById(questionID);

        if (isValidForeignKey(updateSelective.getArticleID())) {
            originalQuestion.setArticleID(updateSelective.getArticleID());
        } else {
            model.addAttribute("error", "Invalid Article ID");
            return "edit-question";
        }

        originalQuestion.setQuestion(updateSelective.getQuestion());
        originalQuestion.setSa(updateSelective.getSa());
        originalQuestion.setSb(updateSelective.getSb());
        originalQuestion.setSc(updateSelective.getSc());
        originalQuestion.setSd(updateSelective.getSd());
        originalQuestion.setCorrectOption(updateSelective.getCorrectOption());
        originalQuestion.setAnalysis(updateSelective.getAnalysis());
        originalQuestion.setScore(updateSelective.getScore());


        questionService.updateSelective(originalQuestion);

        model.addAttribute("message", "Question updated successfully");
        return "redirect:/questions/all2";
    }

    private boolean isValidForeignKey(long articleID) {
        Article article = articleService.getArticleById(articleID);
        return article != null; // 如果 article 不为 null，则表示外键字段有效
    }


    @GetMapping("/delete/{questionID}")
    public String deleteQuestion(@PathVariable int questionID, ModelMap model) {
        questionService.deleteQuestion(questionID);
        model.addAttribute("message", "Question deleted successfully");
        return "redirect:/questions/all2";
    }

    @PostMapping("/compare")
    public String compareAnswers(int questionID, String selectedOption, ModelMap model) {
        boolean isCorrect = questionService.compareAnswers(questionID, selectedOption);
        model.addAttribute("isCorrect", isCorrect);
        return "answerResult";
    }
}
